﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data;

namespace JinianNet.JNCms.SQLServerDAL
{
    public class DataProcess : Data.IDataProcess
    {
        //private static object lockHelper;

        static DataProcess()
        {
            //lockHelper = new object();
        }

        private JinianNet.Data.DbHelper db;
        public JinianNet.Data.DbHelper Helper
        {
            get { return this.db; }
        }

        private JinianNet.Reflection.EntityHelper entity;
        public JinianNet.Reflection.EntityHelper Entity
        {
            get { return this.entity; }
        }

        public DataProcess()
        {
            this.db = new SqlHelper();
            this.entity = new JinianNet.Reflection.EntityHelper();
        }

        public int CreateKey()
        {
            return CreateKey(this.GetType());
        }

        public int CreateKey(Type type){
            return CreateKey(type.Name);
        }

        public int CreateKey(string key)
        {
            int value;
            //lock (lockHelper)
            {
                SqlParameter parameters = new SqlParameter("@ID",SqlDbType.VarChar);
                parameters.Value = key;
                db.ExecuteNonQuery(CommandType.Text, "update JNCms_PrimaryManage set [ID]=[ID]+1 where Key=@ID", parameters);
                value = int.Parse(db.ExecuteScalar(CommandType.Text, "select ID from JNCms_PrimaryManage where Key=@ID", parameters).ToString()) + 1;
            }
            return value;
        }

        public DataTable ExecuteTable(string sql)
        {
            return db.ExecuteTable(sql);
        }

        public void Execute(string sql)
        {
            db.ExecuteNonQuery(sql);
        }

        //public override void Create<T>(string tableName,T model)
        //{
        //    if (model == null)
        //        throw new ArgumentNullException("model");
  
        //    Type t = model.GetType();
        //    PropertyInfo[] pinfo = t.GetProperties(BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.GetProperty | BindingFlags.Instance);
        //    int j = pinfo.Length;
        //    if (j > 0)
        //    {
        //        DbParameter[] dbparameters = new DbParameter[j];
        //        string[] fileds = new string[j];
        //        string[] values = new string[j];
        //        for (int i = 0; i < j; i++)
        //        {
        //            dbparameters[i] = new SqlParameter();
        //            dbparameters[i].DbType = db.PrepareDbType(pinfo[i].PropertyType);
        //            dbparameters[i].ParameterName = string.Concat("@Param", i.ToString());
        //            dbparameters[i].Value = pinfo[i].GetValue(model, null);
        //            fileds[i] = pinfo[i].Name;
        //            values[i] = dbparameters[i].ParameterName;
        //        }
        //        if (string.IsNullOrEmpty(tableName))
        //            tableName = t.Name;
        //        db.ExecuteNonQuery(System.Data.CommandType.Text, string.Concat("INSERT INTO [", TablePrefix, t.Name, "] ([", string.Join("],[", fileds), "]) VALUES (", string.Join(",", values), ")"), dbparameters);
        //    }

        //}

        //public override bool Exists(string tableName, QualificationCollection qualification)
        //{
        //    object[] parameters;
        //    string sql = string.Concat("select count(*) from [", TablePrefix, tableName, "] where ", ParserQualification(qualification, out parameters));

        //    return int.Parse(db.ExecuteScalar(sql, parameters).ToString()) > 0;
        //}

        //protected override string ParserQualification(QualificationCollection qc, out object[] parameters)
        //{
        //    int n = qc.Count;
        //    string[] fileds = new string[n];
        //    parameters = new DbParameter[n];

        //    for (int i = 0; i < n; i++)
        //    {
        //        fileds[i] = string.Concat("[", qc[i].Key, "]", Operators(qc[i].Equation), "{",i.ToString(),"}");
        //        parameters[i] = qc[i].Value;
        //    }

        //    if (fileds.Length == 0)
        //        return fileds[0];
        //    return string.Format(qc.Expression, fileds);
        //}

        //public override List<T> GetList<T>(string tableName, string key, int index, int size, QualificationCollection qualification)
        //{
        //    List<T> list = new List<T>();
        //    Type t= typeof(T);
        //    if (string.IsNullOrEmpty(tableName))
        //        tableName = t.Name;
        //    if (index < 1)
        //        index = 1;
        //    object[] parameters;
        //    string sql;
        //    if (size > 0)
        //    {
        //        if(index==1)
        //            sql = string.Concat("select * from [", this.TablePrefix, typeof(T).Name, "] where ", ParserQualification(qualification, out parameters), " limit ",size.ToString());
        //        else
        //            sql = string.Concat("select * from [", this.TablePrefix, typeof(T).Name, "] where ", ParserQualification(qualification, out parameters), " limit ", Convert.ToString(size * (index - 1)), ",", Convert.ToString(size * index));
        //    }
        //    else
        //        sql = string.Concat("select * from [", this.TablePrefix, typeof(T).Name, "] where ", ParserQualification(qualification, out parameters));
        //    using (DbDataReader rd = db.ExecuteReader(sql, parameters))
        //    {
        //        JinianNet.Reflection.EntityHelper helper = new JinianNet.Reflection.EntityHelper();
        //        list = helper.PopulateEntityCollection<T>(rd);
        //    }
        //    return list;
        //}

        //public override T GetItem<T>(string tableName, JinianNet.JNCms.Data.DataProcess.QualificationCollection qualification)
        //{
        //    T model = default(T);
        //    object[] parameters;
        //    if(string.IsNullOrEmpty(tableName))
        //        tableName = typeof(T).Name;

        //    string sql = string.Concat("select count(*) from [", TablePrefix, tableName, "] where ", ParserQualification(qualification, out parameters));
        //    using(IDataReader rd = db.ExecuteReader(sql,parameters)){
        //        if(rd.Read()){
        //            JinianNet.Reflection.EntityHelper helper = new JinianNet.Reflection.EntityHelper();
        //            model = helper.PopulateEntity<T>(rd);
        //        }
        //    }
        //    return model;
        //}
    }
}
